﻿using System;
using System.Collections.Generic;
using TallComponents.Web.Pdf.Annotations;

namespace TallComponents.Web.Pdf
{
   /// <summary>
   /// Collection of pages inside a PDF document. Non-creatable. Obtained through the Pages property of <see cref="Document"/>. 
   /// </summary>
   public class PageCollection : IEnumerable<Page>
   {
      internal PageCollection( Document owner, TallComponents.PDF.PageCollection pages )
      {
         _list = new List<Page>();
         int i = 0;

         foreach( TallComponents.PDF.Page item in pages )
         {
            var links = new List<Link>();
            foreach ( TallComponents.PDF.Annotations.Link link in item.Links )
            {
               var visitor = new WebActionVisitor( owner );
               link.MouseUpActions.Accept( visitor, null );
               var actionScript = visitor.Javascript;

               links.Add( new Link( link.Left, link.Width, link.Top, link.Height, actionScript ) );
            }

            _list.Add( new Page( owner, i, item.Width, item.Height, links ) );
            i++;
         }
      }

      /// <summary>
      /// Gets the number of pages contained in the <see cref="T:TallComponents.Web.Pdf.PageCollection"/>.
      /// </summary>
      /// <value>
      /// The number of pages contained in the <see cref="T:TallComponents.Web.Pdf.PageCollection"/>.
      /// </value>
      public int Count
      {
         get { return _list.Count; }
      }

      /// <summary>
      /// Gets the <see cref="TallComponents.Web.Pdf.Page"/> at the specified index.
      /// </summary>
      /// <value>The <see cref="TallComponents.Web.Pdf.Page"/> at the specified index.</value>
      /// <exception cref="ArgumentOutOfRangeException">Occures if <paramref name="index"/> is less than 0 or greater than <see cref="Count"/>.</exception>
      public Page this[ int index ]
      {
         get
         {
            return _list[ index ];
         }
      }

      /// <summary>
      /// Returns an enumerator that iterates through the collection.
      /// </summary>
      /// <returns>
      /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
      /// </returns>
      public IEnumerator<Page> GetEnumerator()
      {
         return _list.GetEnumerator();
      }

      /// <summary>
      /// Returns an enumerator that iterates through a collection.
      /// </summary>
      /// <returns>
      /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
      /// </returns>
      System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
      {
         return _list.GetEnumerator();
      }

      private readonly IList<Page> _list;
   }
}

